package com.example.administrator.personalmail.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.personalmail.R;
import com.example.administrator.personalmail.model.Campaign;
import com.example.administrator.personalmail.model.HomeCampaign;
import com.squareup.picasso.Picasso;

import java.util.List;

public class HomeAdapter extends RecyclerView.Adapter {
    private static final int VIEW_LEFT = 0;
    private static final int VIEW_RIGHT = 1;
    private List<HomeCampaign> mDatas;
    private HomeCampaignListenner mHomeCampaignListenner;
    private Context mContext;

    public HomeAdapter(List<HomeCampaign> datas,Context context){
        mDatas = datas;
        this.mContext = context;
    }

    public HomeAdapter(){
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType==VIEW_LEFT){
            return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.template_home_cardview2,parent,false));
        }
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.template_home_cardview,parent,false));

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder viewHolder = (ViewHolder) holder;
        HomeCampaign homeCampaign = mDatas.get(position);
        viewHolder.textTitle.setText(homeCampaign.getTitle());

        Picasso.with(mContext).load(homeCampaign.getCpOne().getImgUrl()).into(viewHolder.imageViewBig);
        Picasso.with(mContext).load(homeCampaign.getCpTwo().getImgUrl()).into(viewHolder.imageViewSmallTop);
        Picasso.with(mContext).load(homeCampaign.getCpThree().getImgUrl()).into(viewHolder.imageViewSmallBottom);
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public int getItemViewType(int position) {
        int type=VIEW_LEFT;
        if (position % 2 == 0) type = VIEW_RIGHT;

        return type;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        TextView textTitle;
        ImageView imageViewBig;
        ImageView imageViewSmallTop;
        ImageView imageViewSmallBottom;

        public ViewHolder(View itemView) {
            super(itemView);
            textTitle = (TextView) itemView.findViewById(R.id.text_title);
            imageViewBig = (ImageView) itemView.findViewById(R.id.imgview_big);
            imageViewSmallTop = (ImageView) itemView.findViewById(R.id.imgview_small_top);
            imageViewSmallBottom = (ImageView) itemView.findViewById(R.id.imgview_small_bottom);

            imageViewBig.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mHomeCampaignListenner!=null)
                        mHomeCampaignListenner.homeCampaignClick(view,mDatas.get(getLayoutPosition()-1).getCpOne());
                }
            });
            imageViewSmallTop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mHomeCampaignListenner!=null)
                        mHomeCampaignListenner.homeCampaignClick(view,mDatas.get(getLayoutPosition()-1).getCpTwo());
                }
            });
            imageViewSmallBottom.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mHomeCampaignListenner!=null)
                        mHomeCampaignListenner.homeCampaignClick(view,mDatas.get(getLayoutPosition()-1).getCpThree());
                }
            });
        }
    }

    public interface HomeCampaignListenner{
        void homeCampaignClick(View view, Campaign campaign);
    }

    public void setHomeCampaignListenner(HomeCampaignListenner homeCampaignListenner){
        mHomeCampaignListenner = homeCampaignListenner;
    }
}
